/**
 * Coryright 2015-2020 www.eyaomall.cn
 * All rights reserved.
 * @author huwenqing
 * @date 2016年8月3日
 *
 */
package com.eyaomall.wxmanage.controller;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmanage.common.error.BECode;
import com.eyaomall.wxmanage.common.util.StringUtil;
import com.eyaomall.wxmanage.model.po.User;
import com.eyaomall.wxmanage.service.UserService;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.exception.ErrorCodeException;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 界面
	 */
	@RequestMapping("index")
	public String index(){
		return "user/index";
	}
	
	/**
	 * 新增用户界面
	 */
	@RequestMapping("detail")
	public String detail(Integer id,Map<String,Object> data){
		if(id != null && id > 0){
			User user = userService.queryById(id);
			if(user != null){
				data.put("userVO", user);
			}
		}
		return "user/detail";
	}
	
	/**
	 * 分页查询用户分页信息
	 */
	@RequestMapping("page_data")
	public @ResponseBody GsnData pageData(User user,PageHTTPObject object){
		return GsnResponse.toData(userService.pageUser(user,object)); 
	}
	
	/**
	 * 保存新增用户
	 */
	@RequestMapping("save")
	public @ResponseBody GsnData save(User user){
		boolean flag ;
		if(user.getId()==null || user.getId() <=0){
			flag = userService.createUser(user);
		}else{
			flag = userService.updateUser(user);
		}
		return GsnResponse.toData(flag);
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("del_by_id")
	public @ResponseBody GsnData delById(Integer id){
		return GsnResponse.toData(userService.delById(id));
	}
	
	/**
	 * 重置密码
	 */
	@RequestMapping("update_passwd")
	public @ResponseBody GsnData updatePasswd(Integer id,String passwd,HttpServletRequest request,HttpServletResponse response){
		if(id == null || id <= 0)
			throw new ErrorCodeException(BECode.user_304001);
		
		if(StringUtil.isEmpty(passwd))
			throw new ErrorCodeException(BECode.user_304002);
		
		if(passwd.length() < 6 || passwd.length() > 20)
			throw new ErrorCodeException(BECode.user_304003);
			
		User user = userService.queryById(id);
		if(user == null)
			throw new ErrorCodeException(BECode.user_304004);
		
		user.setPasswd(passwd);
		
		return GsnResponse.toData(userService.updateUser(user));
	}
	/**
	 * 根据账号查询用户是否存在
	 * @param account
	 * @return
	 */
	@RequestMapping("query_account")
	public @ResponseBody GsnData queryAccount(String account,Integer id){
		if(StringUtil.isEmpty(account))
			throw new ErrorCodeException(BECode.user_304005);
		
		User user = userService.queryByAccount(account);
		if(user == null){
			return GsnResponse.toData(true);
		}else{
			if(id == null){
				return GsnResponse.toData(false);
			}else{
				User owner = userService.queryById(id);
				if(owner == null){
					return GsnResponse.toData(false);
				}else if(account.equalsIgnoreCase(owner.getAccount())){
					
					return GsnResponse.toData(true);
				}
			}
		}
		
		return GsnResponse.toData(false);
	}
	
	/**
	 * 根据电话号码查询用户是否存在
	 * @param account
	 * @return
	 */
	@RequestMapping("query_phone")
	public @ResponseBody GsnData queryPhone(String phone,Integer id){
		if(StringUtil.isEmpty(phone))
			throw new ErrorCodeException(BECode.user_304006);
		
		User user = userService.queryByPhone(phone);
		if(user == null){
			
			return GsnResponse.toData(true);
		}else{
			if(id == null){
				return GsnResponse.toData(false);
			}else{
				User owner = userService.queryById(id);
				if(owner == null){
					return GsnResponse.toData(false);
				}else if(phone.equalsIgnoreCase(owner.getPhone())){
					return GsnResponse.toData(true);
				}
			}
		}
		
		return GsnResponse.toData(false);
	}
}
